require.config({
    baseUrl: "/Public/api/",
    waitSeconds: 60
});


/**
* Protect window.console method calls, e.g. console is not defined on IE
* unless dev tools are open, and IE doesn't define console.debug
*/
(function () {

    "use strict";

    var Main = {
        initialized: false,

        initialize: function () {

            if (this.initialized) return;
            this.initialized = true;

            this.build();
         //   this.events();

        },

        build: function () {
            if (!window.console) {
                window.console = {};
            }
            // union of Chrome, FF, IE, and Safari console methods
            var m = [
                "log", "info", "warn", "error", "debug", "trace", "dir", "group",
                "groupCollapsed", "groupEnd", "time", "timeEnd", "profile", "profileEnd",
                "dirxml", "assert", "count", "markTimeline", "timeStamp", "clear"
            ];
            // define undefined methods as noops to prevent errors
            for (var i = 0; i < m.length; i++) {
                if (!window.console[m[i]]) {
                    window.console[m[i]] = function () { };
                }
            }

            //http://jsfiddle.net/sanelibsolutions/M9hcu/
            jQuery.fn.center = function(parent) {
                if (parent) {
                    parent = this.parent();
                } else {
                    parent = window;
                }
                this.css({
                    "position": "absolute",
                    "top": ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop() + "px"),
                    "left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px")
                });
                return this;
            };
        },

        events: function () {

            var $infoElm = $("#infoElm");
            $infoElm.hide();

            $infoElm = $('#infoElm');
            $infoElm.hide();

            $("input").on("focus", function () {
                var elm = $(this).parent();
                var pos = $(elm).offset();
                var h = $(elm).height();
                var w = $(elm).width();

                var infoText = $(elm).attr("data-info");
                var errorText = $(elm).attr("data-error");
                var innerHtml = '';

                if (infoText) {
                    innerHtml += '<li class="list-group-item list-group-item-info">' + infoText + '</div>';
                }

                if (errorText) {
                    innerHtml += '<li class="list-group-item list-group-item-warning">' + errorText + '</div>';
                }

                if (innerHtml.length > 0) {
                    $infoElm.css({ left: pos.left + 10, top: pos.top + h + 2 });
                    $infoElm.html('<ul class="list-group">' + innerHtml + '</ul>');
                    $infoElm.show();
                }
            });

            $("input").on("blur", function () {
                $infoElm.hide();
            });

            setInterval(function () {
                $(window).resize();
            }, 3000);

            $('.alert-bar').fadeIn('slow');

            $('.alert-bar a.close-notify').click(function () {
                $('.alert-bar').fadeOut('fast'); return false;
            });

            $("#withjs").css('visibility', 'visible');

            //Try to use this script everywhere. Need to associate date input.
            $("abbr.timeago").each(function () {
                var self = $(this);
                var dateVal = self.attr("title");
                if (!dateVal) return self.html('');
                var date = new Date(dateVal);
                return self.html(self.attr("prefix") + " " + moment(date).fromNow());
            });

            $('.confirm').click(function () {
                return confirm('The action you are about to take is non-reversible. Are you sure you want to perform this action?');
            });

            // This code enables going back to previous page programatically, by placing the following 
            // attribute in the clickable element's html markup: data-command='goBack'.
            // Example: <a href='#' data-command='goBack'>Cancel</a>
            $('[data-command=goBack]').click(function (e) {
                if (!!e) e.preventDefault();
                window.history.back();
            });

            $.Shortcuts.add({
                type: 'down',
                mask: 'F1',
                handler: function () {
                    alert('show help');
                }
            });

            $.Shortcuts.start();

            $("#inputFile").change(function () {
                $.publish('filesDropped', [document.getElementById("inputFile").files]);
            });

            $(".fileDragHolder").on('dragenter', function (e) {
                e.stopPropagation();
                e.preventDefault();
                $(this).css('border', '2px dotted #0B85A1');
            });

            $(".fileDragHolder").on('dragover', function (e) {
                e.stopPropagation();
                e.preventDefault();
            });

            $(".fileDragHolder").on('drop', function (e) {
                $(this).css('border', '');
                e.preventDefault();
                var files = e.originalEvent.dataTransfer.files;
                $.publish('filesDropped', [files]);

            });

            $(document).on('dragenter', function (e) {
                e.stopPropagation();
                e.preventDefault();
            });

            $(document).on('dragover', function (e) {
                e.stopPropagation();
                e.preventDefault();
            });

            $(document).on('drop', function (e) {
                e.stopPropagation();
                e.preventDefault();
            });
        }
    };

    Main.initialize();

    $(document).ready(function () {

        Main.events();

    });

})();